/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.r.service.impl;

import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.utils.ResourceBundleUtils;
import com.intelligent.ispc.constant.AppConstant;
import com.intelligent.ispc.core.dto.*;
import com.intelligent.ispc.r.analysis.RCommandFactory;
import com.intelligent.ispc.r.analysis.grr.GrrAnovaAndSourceCommand;
import com.intelligent.ispc.r.analysis.grr.GrrChartDataCommand;
import com.intelligent.ispc.r.analysis.grr.GrrConfiguration;
import com.intelligent.ispc.r.analysis.grr.GrrSummaryCommand;
import com.intelligent.ispc.r.analysis.spc.*;
import com.intelligent.ispc.r.dto.AnalysisParamDto;
import com.intelligent.ispc.r.dto.ControlDataDto;
import com.intelligent.ispc.r.dto.NormalDistributionDto;
import com.intelligent.ispc.r.dto.ScatterDto;
import com.intelligent.ispc.r.service.AnalysisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Eligi.Ran on 2016/8/30.
 */
@Service
public class AnalasisServiceImpl implements AnalysisService {
    private final Logger logger = LoggerFactory.getLogger(AnalasisServiceImpl.class);

    @Override
    public NormalDistributionDto analyseNormalDistribution(AnalysisParamDto analysisParamDto, List<CPWarningRuleDto> cpWarningRules) {
        if (analysisParamDto.getAnalysisData() == null) {
            return null;
        }

        NDCommand ndCommand = RCommandFactory.createNDCommand();
        NDConfiguration ndConfiguration = new NDConfiguration();

        ndConfiguration.setCPWarningRules(cpWarningRules);
        ndCommand.setAnalysisParamDto(analysisParamDto);
        ndCommand.setConfiguration(ndConfiguration);

        ndCommand.execute();

        return ndCommand.getResultDto();
    }

    @Override
    public ScatterDto analyseScatter(AnalysisParamDto analysisParamDto, List<CPWarningRuleDto> cpWarningRules) {
        if (analysisParamDto.getAnalysisData() == null) {
            return null;
        }
        ScatterCommand scatterCommand = RCommandFactory.createScatterCommand();
        ScatterConfiguration scatterConfiguration = new ScatterConfiguration();

        scatterConfiguration.setCPWarningRules(cpWarningRules);
        scatterCommand.setAnalysisParamDto(analysisParamDto);
        scatterCommand.setConfiguration(scatterConfiguration);

        scatterCommand.execute();

        return scatterCommand.getResultDto();
    }

    @Override
    public ControlDataDto analyseControlChart(String type, AnalysisParamDto analysisParamDto, List<SpcConfigDto> ruleList) {
        if (type == null || analysisParamDto.getAnalysisData() == null) {
            return null;
        }
        CCCommand ccCommand = null;
        switch (type) {
            case AppConstant.CHART_TYPE_SPC_XBAR:
                ccCommand = RCommandFactory.createXBarCommand();
                break;
            case AppConstant.CHART_TYPE_SPC_MR:
                ccCommand = RCommandFactory.createMRCommand();
                break;
            case AppConstant.CHART_TYPE_SPC_R:
                ccCommand = RCommandFactory.createRCommand();
                break;
            case AppConstant.CHART_TYPE_SPC_RUN:
                ccCommand = RCommandFactory.createRunCommand();
                break;
            case AppConstant.CHART_TYPE_SPC_SD:
                ccCommand = RCommandFactory.createSDCommand();
                break;
            default:
                break;
        }
        ccCommand.setAnalysisParamDto(analysisParamDto);
        ccCommand.setCconfiguration(new CConfiguration(ruleList));
        ccCommand.execute();

        return ccCommand.getResultDto();
    }

    @Override
    public List<GrrSummaryDto> analyseGrrSummary(GrrParamDto grrParamDto, List<GrrAnalysisDataDto> grrAnalysisDataDtos) {
        List<GrrSummaryDto> summaryDtos = null;
        try {
            GrrSummaryCommand command = RCommandFactory.createGrrSummaryCommand();

            command.setAnalysisParams(grrAnalysisDataDtos);
            command.setGrrParamDto(grrParamDto);

            GrrConfiguration configuration = new GrrConfiguration();
            configuration.setDigitNum(grrParamDto.getDigitNum());
            configuration.setRule(grrParamDto.getRule());
            command.setConfiguration(configuration);

            command.execute();
            summaryDtos = command.getResultData();
        } catch (Exception ex) {
            logger.debug(ex.getMessage());
            ex.printStackTrace();
            throw new ApplicationException(ExceptionMessages.ERR_12009, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GRR_ANALYSIS_INVALID));
        }

        return summaryDtos;
    }

    @Override
    public Map<String, Object> analyseGrrAnovaAndSource(GrrParamDto grrParamDto, GrrAnalysisDataDto grrAnalysisDataDto) {
        Map<String, Object> resultMap = null;
        try {
            GrrAnovaAndSourceCommand command = RCommandFactory.createGrrAnovaAndSourceCommand();

            command.setAnalysisParam(grrAnalysisDataDto);
            command.setGrrParamDto(grrParamDto);

            GrrConfiguration configuration = new GrrConfiguration();
            configuration.setDigitNum(grrParamDto.getDigitNum());
            configuration.setRule(grrParamDto.getRule());
            command.setConfiguration(configuration);

            command.execute();
            resultMap = command.getResultData();
        } catch (Exception ex) {
            logger.debug(ex.getMessage());
            ex.printStackTrace();
            throw new ApplicationException(ExceptionMessages.ERR_12009, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GRR_ANALYSIS_INVALID));
        }

        return resultMap;
    }

    @Override
    public GrrChartDto analyseGrrChartData(GrrParamDto grrParamDto, GrrAnalysisDataDto grrAnalysisDataDto) {
        GrrChartDto resultDto = null;
        try {
            GrrChartDataCommand command = RCommandFactory.createGrrChartDataCommand();

            command.setAnalysisParam(grrAnalysisDataDto);
            command.setGrrParamDto(grrParamDto);

            GrrConfiguration configuration = new GrrConfiguration();
            configuration.setDigitNum(AppConstant.ANALYSIS_GRR_DIGNUM);
            configuration.setRule(grrParamDto.getRule());
            command.setConfiguration(configuration);

            command.execute();
            resultDto = command.getResultData();
        } catch (Exception ex) {
            logger.debug(ex.getMessage());
            ex.printStackTrace();
            throw new ApplicationException(ExceptionMessages.ERR_12009, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GRR_ANALYSIS_INVALID));
        }

        return resultDto;
    }

    @Override
    public void analyseGrrChartPlot(GrrParamDto grrParamDto, GrrAnalysisDataDto grrAnalysisDataDto) {

    }
}
